package com.snail.xgcd.controller;

import com.snail.xgcd.bean.Single;
import com.snail.xgcd.bean.User;
import com.snail.xgcd.service.UserService;
import com.snail.xgcd.service.annotation.Authorization;
import com.snail.xgcd.service.annotation.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public Single login(@RequestBody User user){
        return userService.login(user);
    }

    @RequestMapping(value = "/logout",method = RequestMethod.DELETE)
    @Authorization
    public Single logout(@CurrentUser User user) {
        return userService.logout(user);
    }

    @RequestMapping(value = "/list")
    @Authorization
    public Single list(User user,Integer pageSize,Integer pageNum){
        return userService.findAll(user,pageSize,pageNum);
    }

    @RequestMapping("/del")
    public Single del(String id){
        return userService.deleteByPrimaryKey(id);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Single add(@RequestBody User user){
        return userService.insert(user);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Single update(@RequestBody User user){
        return userService.updateByPrimaryKeySelective(user);
    }

}
